package com.k1998.mybatis.controller;

import com.alibaba.fastjson.JSON;
import com.k1998.mybatis.utils.QueryInfo;
import com.k1998.mybatis.bean.StudentClass;
import com.k1998.mybatis.dao.IStudentClassDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = {"专业班级Api文档"})
public class StudentClassController {
    @Autowired
    private IStudentClassDao iStudentClassDao;
    @PostMapping("/allStudentClass")
    public String getAll(@RequestBody QueryInfo queryInfo) {
        // 查询到的结果数量
        int numbers = iStudentClassDao.getCounts("%"+queryInfo.getQuery()+"%");
        // 第几条数据开始
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<StudentClass> students = iStudentClassDao.findAll("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",students);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/detailStudentClass")
    @ApiOperation(value = "详细查询", notes="详细查询", httpMethod = "POST")
    public String detailStudent(@RequestBody StudentClass studentClass){
        StudentClass s = iStudentClassDao.detailStudentClass(studentClass);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",s);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/insertStudentClass")
    @ApiOperation(value = "添加专业班级", notes="添加专业班级", httpMethod = "POST")
    public String insertStudent(@RequestBody StudentClass studentClass){
        int i = iStudentClassDao.insertStudentClass(studentClass);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/updateStudentClass")
    @ApiOperation(value = "修改专业班级", notes="修改专业班级", httpMethod = "POST")
    public String updateStudent(@RequestBody StudentClass studentClass){
        int i = iStudentClassDao.updateStudentClass(studentClass);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/deleteStudentClass")
    @ApiOperation(value = "删除专业班级", notes="删除专业班级", httpMethod = "POST")
    public String deleteStudent(@RequestBody StudentClass studentClass){
        int i = iStudentClassDao.deleteStudentClass(studentClass);
        String str = i >0?"success":"error";
        return str;
    }

}
